
<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" label-width="auto" :inline="true">
        <el-form-item label="供应商" prop="supplierId">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.supplierId" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in supplierOptions" :key="item.id" :label="item.companyName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="供应商编号" prop="supplierNo">
          <el-input v-model="queryParams.supplierNo" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="提现账户名" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="平台" prop="platformId">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.platformId" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in platFormArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="渠道" prop="channelId">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.channelId" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in MychannelArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="statusList">
          <el-select multiple clearable @keyup.enter="handleQuery" v-model="queryParams.statusList" placeholder="请选择" style="width: 256px">
            <el-option :value="0" label="待审核" />
            <el-option :value="1" label="通过" />
            <el-option :value="2" label="驳回" />
          </el-select>
        </el-form-item>
        <el-form-item label="附件状态" prop="enclosureStatus">
          <el-select clearable @keyup.enter="handleQuery" v-model="queryParams.enclosureStatus" placeholder="请选择" style="width: 256px">
            <el-option :value="0" label="待确认" />
            <el-option :value="1" label="已确认" />
          </el-select>
        </el-form-item>
        <el-form-item label="账单时间" prop="billingTime">
          <el-date-picker style="width: 256px" v-model="queryParams.billingTime" type="month" format="YYYYMM" valueFormat="YYYYMM" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="审核时间">
          <el-date-picker
            style="width: 16rem"
            format="YYYY/MM/DD"
            value-format="YYYY-MM-DD"
            @change="handleUseTimeChange"
            v-model="updateTimes"
            type="daterange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon><Search /></template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon><Refresh /></template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <div class="upload_box">
          <el-button type="warning" preIcon="ant-design:export-outlined" @click="handleExports">导出</el-button>
          <el-button :disabled="ids.length === 0" @click="batchHandleDelete(1)" type="primary">
            <template #icon><Delete /></template>
            批量通过
          </el-button>
          <el-button :disabled="ids.length === 0" @click="batchHandleDelete(2)" type="danger">
            <template #icon><Delete /></template>
            批量拒绝
          </el-button>
          <div v-if="totalPrice" style="display: flex; align-items: center">
            <div>总金额</div>
            <div style="color: red">{{ totalPrice }}</div>
          </div>
        </div>
      </template>
      <el-table v-loading="loading" :data="TableData" @selection-change="handleSelectionChange" highlight-current-row row-key="id">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="供应商" min-width="300" align="center" prop="supplierName">
          <template #default="scope">
            <div>{{ scope.row.supplierNo }}-{{ scope.row.supplierName }}</div>
          </template>
        </el-table-column>
        <el-table-column label="平台" min-width="120" align="center" prop="platformId">
          <template #default="scope">
            <div>{{ platFormEnum[scope.row.platformId] }}</div>
          </template>
        </el-table-column>
        <el-table-column label="渠道" min-width="120" align="center" prop="channelId">
          <template #default="scope">
            <div v-if="scope.row.channelId == 0">全部</div>
            <div v-else>{{ channelEnum[scope.row.channelId] }}</div>
          </template>
        </el-table-column>
        <el-table-column label="提现账户" min-width="120" align="center" prop="account"></el-table-column>
        <el-table-column label="提现账户名" min-width="120" align="center" prop="name"></el-table-column>
        <el-table-column label="金额" min-width="230" align="center" prop="fund">
          <!-- <template #default="scope">
            <div style="display: flex; align-items: center; justify-content: center" v-if="scope.row.isSelected">
              <el-input style="width: 40%" v-model="scope.row.fund" @focus="focusEvent(scope.row)" @blur="blurEvent(scope.row)" v-focus></el-input>
              <el-button type="primary" @click="blurEvent(scope.row)">修改</el-button>
            </div>
            <p @click="cellClick(scope.row)" v-else>{{ scope.row.fund }}</p>
          </template> -->
        </el-table-column>
        <el-table-column label="开票金额" min-width="120" align="center" prop="supplierFund"></el-table-column>
        <el-table-column label="扣款金额" min-width="120" align="center" prop="cutFund"></el-table-column>
        <el-table-column label="扣款备注" min-width="120" align="center" prop="cutRemark"></el-table-column>
        <el-table-column label="附件" min-width="120" align="center" prop="enclosure">
          <template #default="scope">
            <a :href="scope.row.enclosure" style="color: blue">下载</a>
          </template>
        </el-table-column>
        <el-table-column label="附件状态" min-width="120" align="center" prop="enclosureStatus">
          <template #default="scope">
            <div v-if="scope.row.enclosureStatus == 0">待确认</div>
            <div v-if="scope.row.enclosureStatus == 1">已确认</div>
          </template>
        </el-table-column>

        <el-table-column label="状态" min-width="120" align="center" prop="status">
          <template #default="scope">
            <div style="color: orange" v-if="scope.row.status == 0">待处理</div>
            <div style="color: #55d187" v-else-if="scope.row.status == 1">成功</div>
            <div style="color: red" v-else>失败</div>
          </template>
        </el-table-column>
        <el-table-column label="原因" min-width="120" align="center" prop="reason"></el-table-column>
        <el-table-column label="创建时间" min-width="180" align="center" prop="createTime"></el-table-column>
        <el-table-column label="审核时间" min-width="180" align="center" prop="updateTime"></el-table-column>
        <el-table-column label="账单时间" min-width="140" align="center" prop="billingTime"></el-table-column>
        <el-table-column fixed="right" align="center" label="操作" width="220">
          <template #default="scope">
            <div>
              <el-button v-hasPerm="['supplier/audit']" type="primary" link size="small" @click.stop="handleMoney(scope.row)">
                <template #icon><Pointer /></template>
                审核
              </el-button>
              <el-button type="primary" link size="small" @click.stop="handleKouKuan(scope.row)">
                <template #icon><Pointer /></template>
                扣款
              </el-button>
              <el-button v-if="scope.row.enclosureStatus != 1" v-hasPerm="['supplier/audit']" type="warning" link size="small" @click.stop="handleConfirmEnclosure(scope.row)">
                <template #icon><Pointer /></template>
                确认附件
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
    </el-card>
    <!-- 改派 -->
    <MoneyModal ref="modalRef" @success="handleQuery" />
    <MoneyModal2 ref="modalRef2" @success="handleQuery" />
    <el-dialog v-model="isShoeConfirmAudit" title="审核" width="600" :before-close="handleClose">
      <div style="display: flex; align-items: center; padding: 40px 0">
        <div style="margin-right: 14px">原因:</div>
        <el-input style="width: 400px" v-model="reason" placeholder="请输入"></el-input>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确认</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import CommonApi from "@/api/common";
import { ref, onMounted, reactive } from "vue";
import { platFormEnum, channelEnum, platFormArr, channelArr } from "@/enums/common";
import { MenuTypeEnum } from "@/enums/MenuTypeEnum";
import ExportApi from "@/api/exports";
import MoneyApi from "@/api/money";
import MoneyModal from "./components/moneyModal.vue";
import MoneyModal2 from "./components/moneyModal2.vue";

const queryFormRef = ref(ElForm);
const total = ref(0);
const supplierOptions = ref();
const modalRef = ref(null);
const modalRef2 = ref(null);
const cityOptions = ref();
const MychannelArr = ref();
const loading = ref(false);
const totalPrice = ref();
const ids = ref<number[]>([]);
const isShoeConfirmAudit = ref(false);
const reason = ref();
const updateTimes = ref();

function focusEvent(row: any) {
  row.oldFund = row.fund;
}

function blurEvent(row) {
  row.isSelected = !row.isSelected;
  if (row.oldFund !== row.fund) {
    ElMessageBox.confirm(`确认修改金额为${row.fund}吗?`, "警告", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(() => {
      MoneyApi.updateSupplierMoney({ fund: row.fund, id: row.id }).then((res) => {
        if (res.code == 0) {
          ElMessage.success(res.message);
          handleQuery();
        }
      });
    });
  }
}
function cellClick(row) {
  row.isSelected = !row.isSelected;
}

function handleKouKuan(row) {
  modalRef2.value.edit(row);
}
//点击确认附件出发
function handleConfirmEnclosure(row: any) {
  ElMessageBox.confirm("确认附件无误?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
    function () {
      MoneyApi.enclosureConfirm({ id: row.id })
        .then(() => {
          ElMessage.success("成功");
          handleQuery();
        })
        .finally(() => (loading.value = false));
    },
    function () {
      ElMessage.info("已取消");
    }
  );
}
// 点击供应商提现触
function handleMoney(row: any) {
  modalRef.value.edit(row);
}
function handleExports() {
  ExportApi.supplierWithdrawExport(queryParams).then((res) => {
    let data = res.data;
    ElMessage.success("导出完毕");
    if (!data) {
      return;
    }
    if (typeof window.navigator.msSaveBlob !== "undefined") {
      window.navigator.msSaveBlob(new Blob([data], { type: "application/vnd.ms-excel" }), fileName + ".xls");
    } else {
      let url = window.URL.createObjectURL(new Blob([data], { type: "application/vnd.ms-excel" }));
      let link = document.createElement("a");
      link.style.display = "none";
      link.href = url;
      link.setAttribute("download", "供应商账单" + ".xlsx");
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link); //下载完成移除元素
      window.URL.revokeObjectURL(url); //释放掉blob对象
    }
  });
}

/** 批量通过||拒绝 */
async function batchHandleDelete(type?: number) {
  if (type == 1) {
    //发送审核请求
    ElMessageBox.confirm("确认审核?", "警告", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(() => {
      let data = {
        ids: ids.value,
        op: type,
      };
      MoneyApi.supplierApproval(data).then((res: any) => {
        if (res.code == 0) {
          ElMessage.success(res.message);
          handleQuery();
        }
      });
    });
  } else {
    isShoeConfirmAudit.value = true;
  }
}

function handleClose() {
  isShoeConfirmAudit.value = false;
  reason.value = "";
}
function handleSubmit() {
  let params = {
    ids: ids.value,
    op: 2,
    reason: reason.value,
  };
  MoneyApi.supplierApproval(params).then((res: any) => {
    if (res.code == 0) {
      //发送审核请求
      isShoeConfirmAudit.value = false;
      ElMessage.success(res.message);
      handleQuery();
      reason.value = "";
    } else {
      ElMessage.error(res.message);
    }
  });
}

/** 行复选框选中记录选中ID集合 */
function handleSelectionChange(selection: any) {
  let sum = 0;
  selection.forEach((item: any) => {
    sum += item.fund;
  });
  if (sum > 0) {
    totalPrice.value = sum.toFixed(1);
  } else {
    totalPrice.value = "";
  }

  ids.value = selection.map((item: any) => item.id);
}

// 查询参数
const queryParams = reactive<MenuQuery>({
  pageNo: 1,
  pageSize: 10,
});
// 菜单表格数据
const TableData = ref([]);

function handleUseTimeChange(time: any) {
  if (time) {
    queryParams.updateTimeFrom = `${time[0]} 00:00:00`;
    queryParams.updateTimeTo = `${time[1]} 23:59:59`;
  } else {
    queryParams.updateTimeFrom = undefined;
    queryParams.updateTimeTo = undefined;
  }
}

// 查询菜单
function handleQuery() {
  loading.value = true;
  MoneyApi.getSupplierList(queryParams)
    .then((data) => {
      TableData.value = data.result;
      total.value = data.total * 1;
    })
    .finally(() => {
      loading.value = false;
    });
}
import router from "@/router";

// 重置查询
function handleResetQuery() {
  queryParams.updateTimeFrom = undefined;
  queryParams.updateTimeTo = undefined;
  updateTimes.value = null;
  queryFormRef.value.resetFields();
  queryParams.pageNo = 1;
  handleQuery();
}

onMounted(() => {
  MychannelArr.value = JSON.parse(JSON.stringify(channelArr));
  MychannelArr.value.unshift({ label: "全部", value: 0 });
  handleQuery();
  CommonApi.getSupplier({ pageNo: 1, pageSize: 9999, status: 1 }).then((data) => {
    if (data.code == 0) {
      supplierOptions.value = data.result;
    }
  });
  CommonApi.getCity({ pageNo: 1, pageSize: 9999 }).then((data) => {
    if (data.code == 0) {
      cityOptions.value = data.result;
    }
  });
});
</script>
<style lang="scss" scoped>
.upload_box {
  display: flex;
  align-items: center;
  .upload-demo {
    margin: 0 4px;
  }
}
.red_rate {
  color: red;
}
.address_box {
  display: flex;
  flex-direction: column;
  align-items: left;
  justify-content: left;
  .item {
    display: flex;
    align-items: center;
    margin: 4px 0;
    .tag {
      width: 50px;
      height: 23px;
      text-align: center;
      color: orange;
      background: #fff8e6;
    }
    .tag2 {
      color: #3fd175;
      background: #e7faf0;
    }
  }
}
</style>
